package com.javasm.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.javasm.entity.LoginUser;
import com.javasm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Properties;

@RestController
public class SecController {
    @GetMapping("/code")
    public void getVerifyCode(HttpServletResponse resp, HttpSession session) throws IOException {
        //验证码配置
        Properties properties = new Properties();
        properties.setProperty("kaptcha.image.width", "150");
        properties.setProperty("kaptcha.image.height", "50");
        properties.setProperty("kaptcha.textproducer.char.string", "0123456789");
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        Config config = new Config(properties);
        DefaultKaptcha kaptcha = new DefaultKaptcha();
        kaptcha.setConfig(config);

        //生成验证码
        String text = kaptcha.createText();
        //放到session中
        session.setAttribute("verify_code", text);
        //返回给前端
        resp.setContentType("image/jpeg");
        BufferedImage image = kaptcha.createImage(text);
        try(ServletOutputStream out = resp.getOutputStream()) {
            ImageIO.write(image, "jpg", out);
        }
    }
    @GetMapping("/phone/code")
    public String phoneCode(HttpSession session) throws IOException {
        //验证码配置
        Properties properties = new Properties();
        properties.setProperty("kaptcha.image.width", "150");
        properties.setProperty("kaptcha.image.height", "50");
        properties.setProperty("kaptcha.textproducer.char.string", "0123456789");
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        Config config = new Config(properties);
        DefaultKaptcha kaptcha = new DefaultKaptcha();
        kaptcha.setConfig(config);

        //生成验证码
        String code = kaptcha.createText();
        session.setAttribute("phoneNum", code);
//        session.setAttribute("Num", code);
        return code;
    }


    @GetMapping("/sec")
    public String sec(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser currentUser = (LoginUser) authentication.getPrincipal();
        return JSONObject.toJSONString(currentUser);
    }
    @Autowired
    private UserService userService;
    @GetMapping("/modify")
    public String modify(@RequestParam String username, @RequestParam String password){
        userService.updatePassword(username, password);
        return "success";
    }
}

